<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="close" @open="open">
    <!-- 表单 -->
    <el-form ref="fm" label-width="120px" :model="list" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="list.username" style="width:50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="list.mobile" style="width:50%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="list.timeOfEntry" style="width:50%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="list.formOfEmployment" style="width:50%" placeholder="请选择">
          <el-option
            v-for="item in employments"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>

      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="list.workNumber" style="width:50%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="list.departmentName" style="width:50%" placeholder="请选择部门" @focus="focus" />
        <el-row v-show="isShow" class="deptBox">
          <el-tree :data="data" :props="{label:'name'}" @node-click="handleNodeClick" />
        </el-row>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="list.correctionTime" style="width:50%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-button>取消</el-button>
      <el-button type="primary" @click="clickaddFn">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { addxinzen } from '@/api/yuangongguanli'
import { organizational } from '@/api/deeptress'
// 导入转换树形数据的方法
import { listToTreeData } from '@/utils'

export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      employments: [
        {
          id: 1,
          value: '正式'
        },
        {
          id: 2,
          value: '非正式'
        }
      ],
      // 显示隐藏  部门的框
      isShow: false,
      // list
      list: {
        username: '', // 员工名字
        mobile: '', // 请输入手机号
        formOfEmployment: '', // 员工聘用形式
        workNumber: '', // 员工工号
        departmentName: '', // 请选择部门
        timeOfEntry: '', // 员工入职日期
        correctionTime: ''// 员工转正日期

      },
      //
      data: [],
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: ['blur', 'change'] }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: ['blur', 'change'] },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: ['blur', 'change'] }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: ['blur', 'change'] }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: ['blur', 'change'] }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    // 点击确定时触发
    clickaddFn() {
      this.$refs.fm.validate(async(val) => {
        if (val) {
          await addxinzen(this.list)
          this.close()
        } else {
          return false
        }
      })
    },
    // 打开时调用
    open() {
      this.huoqu()
    },
    // 关闭时调用
    close() {
      this.$emit('close')
    },
    // 点击树状图触发
    handleNodeClick(data) {
      this.list.departmentName = data.name
      this.isShow = false
    },
    // 获取
    async huoqu() {
      const { depts } = await organizational()
      // 导入转成树状图的方法
      this.data = listToTreeData(depts)
    },
    // 获取焦点显示
    focus() {
      this.isShow = true
    }
  }

}
</script>

<style lang="scss" scoped>
.deptBox {
  width: 299.8px;
  border: 1px solid #ddd;
}
</style>
